package com.example.heathy_me.medicineDetail;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.heathy_me.MyApplication;
import com.example.heathy_me.R;
import com.example.heathy_me.homeDetail.Medicine_List;
import com.example.heathy_me.utils.RequestHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MedicineTitleFragment extends Fragment {


    private medicineDatabaseHelper dbHelper = new medicineDatabaseHelper(getContext(),"Medicine.db",null,1);
    private String medicineTitle="mm";
    private String[] dbtitle={"title","content"};
    MyApplication app;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.medicine_title_frag, container, false);
        RecyclerView medicineTitleRecyclerView = (RecyclerView) view.findViewById(R.id.medicine_title_recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        medicineTitleRecyclerView.setLayoutManager(layoutManager);
        MedicineAdapter adapter = new MedicineAdapter(getMedicine());
        medicineTitleRecyclerView.setAdapter(adapter);
        return view;
    }


    public void onAttach(Context context) {
        super.onAttach(context);
        medicineTitle=((Medicine_List)context).getTitles();
        app = (MyApplication) ((Medicine_List)context).getApplication();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        medicineTitle = getActivity().getIntent().getStringExtra("medicine_title"); // 获取传入的标题
    }

    private String getresult(String medicineTitle){
        String result="unchanged";
        RequestHelper r = new RequestHelper();
        String json = "";
        Map map = new HashMap();
        map.put("name", medicineTitle);
        JSONObject obj = new JSONObject(map);
        try {
            json=r.register(app.getUrl()+"medicine/",obj.toString());
            Log.d("MainActivity", "json:" + json);
            Toast.makeText(getContext(), "成功！", Toast.LENGTH_LONG).show();

        } catch (IOException e) {
            e.printStackTrace();
        }

        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject(json);
            result = jsonObject.getString("warning");
            Toast.makeText(getContext(), "success", Toast.LENGTH_LONG).show();

        } catch (JSONException e) {
            Toast.makeText(getContext(), "fail", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
        return result;
    }

    private List<Medicine> getMedicine() {
       List<Medicine> medicineList = new ArrayList<>();
            Medicine medicine = new Medicine();
            medicine.setTitle(medicineTitle);
            medicine.setContent(getresult(medicineTitle) );
            medicineList.add(medicine);

        return medicineList;
    }

//    private String getRandomLengthContent(String content) {
//        Random random = new Random();
//        int length = random.nextInt(20) + 1;
//        StringBuilder builder = new StringBuilder();
//        for (int i = 0; i < length; i++) {
//            builder.append(content);
//        }
//        return builder.toString();
//    }

    class MedicineAdapter extends RecyclerView.Adapter<MedicineAdapter.ViewHolder> {

        private List<Medicine> mMedicineList;

        class ViewHolder extends RecyclerView.ViewHolder {

            TextView medicineTitleText;

            public ViewHolder(View view) {
                super(view);
                medicineTitleText = (TextView) view.findViewById(R.id.medicine_title);
            }

        }

        public MedicineAdapter(List<Medicine> medicineList) {
            mMedicineList = medicineList;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.medicine_items, parent, false);
            final ViewHolder holder = new ViewHolder(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Medicine medicine = mMedicineList.get(holder.getPosition());
                    MedicineContentActivity.actionStart(getActivity(), medicine.getTitle(), medicine.getContent());

                }
            });
            return holder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            Medicine medicine = mMedicineList.get(position);
            holder.medicineTitleText.setText(medicine.getTitle());
        }

        @Override
        public int getItemCount() {
            return mMedicineList.size();
        }

    }

}
